home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / networking / otdumpportregistry / otdumpportregistry.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  8.2 KB  |  270 lines

  1. /*
  2.     File:        OTDumpPortRegistry.c
  3.  
  4.     Contains:    A simple program to display OT's list of registered ports.
  5.  
  6.     Written by: Quinn "The Eskimo!"    
  7.  
  8.     Copyright:    Copyright © 1997-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/22/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23. #include <Gestalt.h>
  24. #include <OpenTransport.h>
  25. #include <OpenTptInternet.h>
  26. #include <OpenTptAppleTalk.h>
  27. #include <OpenTptLinks.h>
  28. #include <OpenTptConfig.h>
  29. #include <OpenTptISDN.h>
  30.  
  31. #include <stdio.h>
  32.  
  33. enum {
  34.     kNumDeviceTypeNames = 20
  35. };
  36.  
  37. static char *gDeviceTypeNames[kNumDeviceTypeNames] = {
  38. "kOTNoDeviceType",
  39. "kOTADEVDevice",
  40. "kOTMDEVDevice",
  41. "kOTLocalTalkDevice",
  42. "kOTIRTalkDevice",
  43. "kOTTokenRingDevice",
  44. "kOTISDNDevice",
  45. "kOTATMDevice",
  46. "kOTSMDSDevice",
  47.  
  48. "kOTSerialDevice",
  49. "kOTEthernetDevice",
  50. "kOTSLIPDevice",
  51. "kOTPPPDevice",
  52. "kOTModemDevice",
  53. "kOTFastEthernetDevice",
  54. "kOTFDDIDevice",
  55. "kOTIrDADevice",
  56. "kOTATMSNAPDevice",
  57. "kOTFibreChannelDevice",
  58. "kOTFireWireDevice"
  59. };
  60.  
  61. static void PrintFlag(char *flagName, UInt32 flagField, UInt32 flagMask)
  62. {
  63.   printf("  %s = %d\n", flagName, (flagField & flagMask) != 0);
  64. }
  65.  
  66. static void DumpPortRecord(OTPortRecord portInfoRecord)
  67. {
  68.   OTPortRecord    childPortInfoRecord;
  69.     UInt16                deviceType;
  70.     UInt16                busType;
  71.     UInt16                slotNumber;
  72.     UInt16                slotOtherInfo;
  73.     SInt32                childIndex;
  74.     Str255                userFriendlyName;
  75.  
  76.     /* Fields in an OTPortRecord...
  77.     
  78.         OTPortRef        fRef;
  79.         UInt32            fPortFlags;
  80.         UInt32            fInfoFlags;
  81.         UInt32            fCapabilities;
  82.         size_t            fNumChildPorts;
  83.         OTPortRef*    fChildPorts;
  84.         char                fPortName[kMaxProviderNameSize];
  85.         char                fModuleName[kMaxModuleNameSize];
  86.         char                fSlotID[kMaxSlotIDSize];
  87.         char                fResourceInfo[kMaxResourceInfoSize];
  88.         char                fReserved[164];
  89.     */
  90.     
  91.     // Information we can get from the port reference, fRef
  92.     
  93.     busType = OTGetBusTypeFromPortRef(portInfoRecord.fRef);
  94.     deviceType = OTGetDeviceTypeFromPortRef(portInfoRecord.fRef);
  95.     slotNumber = OTGetSlotFromPortRef(portInfoRecord.fRef, &slotOtherInfo);
  96.     
  97.     // Let's print some info!!!
  98.     
  99.     printf("••• Dumping information for port “%s”.\n\n", portInfoRecord.fPortName);
  100.  
  101.     printf("fPortFlags...\n");
  102.     PrintFlag("kOTPortIsActive", portInfoRecord.fPortFlags, kOTPortIsActive);
  103.     PrintFlag("kOTPortIsDisabled", portInfoRecord.fPortFlags, kOTPortIsDisabled);
  104.     PrintFlag("kOTPortIsUnavailable", portInfoRecord.fPortFlags, kOTPortIsUnavailable);
  105.     PrintFlag("kOTPortIsOffline", portInfoRecord.fPortFlags, kOTPortIsOffline);
  106.     printf("\n");
  107.     
  108.     printf("fInfoFlags = 0x%08X...\n", portInfoRecord.fInfoFlags);
  109.     PrintFlag("kOTPortIsDLPI", portInfoRecord.fInfoFlags, kOTPortIsDLPI);
  110.     PrintFlag("kOTPortIsTPI", portInfoRecord.fInfoFlags, kOTPortIsTPI);
  111.     PrintFlag("kOTPortCanYield", portInfoRecord.fInfoFlags, kOTPortCanYield);
  112.     PrintFlag("kOTPortCanArbitrate", portInfoRecord.fInfoFlags, kOTPortCanArbitrate);
  113.     PrintFlag("kOTPortIsTransitory", portInfoRecord.fInfoFlags, kOTPortIsTransitory);
  114.     PrintFlag("kOTPortAutoConnects", portInfoRecord.fInfoFlags, kOTPortAutoConnects);
  115.     PrintFlag("kOTPortIsSystemRegistered", portInfoRecord.fInfoFlags, kOTPortIsSystemRegistered);
  116.     PrintFlag("kOTPortIsPrivate", portInfoRecord.fInfoFlags, kOTPortIsPrivate);
  117.     PrintFlag("kOTPortIsAlias", portInfoRecord.fInfoFlags, kOTPortIsAlias);
  118.     printf("\n");
  119.  
  120.     printf("fCapabilities = 0x%08X\n", portInfoRecord.fCapabilities);
  121.     switch (deviceType) {
  122.         case kOTEthernetDevice:
  123.         case kOTFastEthernetDevice:
  124.             PrintFlag("kOTFramingEthernet", portInfoRecord.fCapabilities, kOTFramingEthernet);
  125.             PrintFlag("kOTFramingEthernetIPX", portInfoRecord.fCapabilities, kOTFramingEthernetIPX);
  126.             PrintFlag("kOTFraming8023", portInfoRecord.fCapabilities, kOTFraming8023);
  127.             PrintFlag("kOTFraming8022", portInfoRecord.fCapabilities, kOTFraming8022);
  128.             break;
  129.         case kOTISDNDevice:
  130.             PrintFlag("kOTISDNFramingTransparentSupported", portInfoRecord.fCapabilities, kOTISDNFramingTransparentSupported);
  131.             PrintFlag("kOTISDNFramingHDLCSupported", portInfoRecord.fCapabilities, kOTISDNFramingHDLCSupported);
  132.             PrintFlag("kOTISDNFramingV110Supported", portInfoRecord.fCapabilities, kOTISDNFramingV110Supported);
  133.             PrintFlag("kOTISDNFramingV14ESupported", portInfoRecord.fCapabilities, kOTISDNFramingV14ESupported);
  134.             break;
  135.         case kOTSerialDevice:
  136.             PrintFlag("kOTSerialFramingAsync", portInfoRecord.fCapabilities, kOTSerialFramingAsync);
  137.             PrintFlag("kOTSerialFramingHDLC", portInfoRecord.fCapabilities, kOTSerialFramingHDLC);
  138.             PrintFlag("kOTSerialFramingSDLC", portInfoRecord.fCapabilities, kOTSerialFramingSDLC);
  139.             PrintFlag("kOTSerialFramingAsyncPackets", portInfoRecord.fCapabilities, kOTSerialFramingAsyncPackets);
  140.             break;
  141.         default:
  142.             break;
  143.     }
  144.     printf("\n");
  145.     
  146.     if (portInfoRecord.fNumChildPorts == 0) {
  147.         printf("No child ports.\n");
  148.     } else {
  149.       printf("fChildPorts...\n");
  150.       for (childIndex = 0; childIndex < portInfoRecord.fNumChildPorts; childIndex++) {
  151.           if (OTFindPortByRef(&childPortInfoRecord, portInfoRecord.fChildPorts[childIndex])) {
  152.                 printf("  %s\n", childPortInfoRecord.fPortName);
  153.           } else {
  154.               printf("That's very strange.\n");
  155.           };
  156.       };
  157.     };
  158.     printf("\n");
  159.           
  160.     printf("fModuleName = %s\n", portInfoRecord.fModuleName);
  161.     printf("fSlotID = %s (only set on PCI ports).\n", portInfoRecord.fSlotID);
  162.     printf("fResourceInfo = %s (optional config library name).\n", portInfoRecord.fResourceInfo);
  163.  
  164.     // extra information gleaned from port ref
  165.     
  166.     printf("fPortRef = %08x\n\n", portInfoRecord.fRef);
  167.  
  168.     printf("Bus type is ");
  169.     switch (busType) {
  170.         case kOTUnknownBusPort:
  171.             printf("kOTUnknownBusPort");
  172.             break;
  173.         case kOTMotherboardBus:
  174.             printf("kOTMotherboardBus");
  175.             break;
  176.         case kOTNuBus:
  177.             printf("kOTNuBus");
  178.             break;
  179.         case kOTPCIBus:
  180.             printf("kOTPCIBus");
  181.             break;
  182.         case kOTGeoPort:
  183.             printf("kOTGeoPort");
  184.             break;
  185.         case kOTPCCardBus:
  186.             printf("kOTPCCardBus");
  187.             break;
  188.         case kOTFireWireBus:
  189.             printf("kOTFireWireBus");
  190.             break;
  191.         default:
  192.             printf("unknown");
  193.     };
  194.     printf(" (%d).\n", busType);
  195.  
  196.     printf("Device type is ");
  197.     if (deviceType >= 0 && deviceType < kNumDeviceTypeNames) {
  198.         printf("%s", gDeviceTypeNames[deviceType]);
  199.     } else {
  200.         if (deviceType == kOTPseudoDevice) {
  201.             printf("kOTPseudoDevice");
  202.         } else {
  203.             printf("unknown");
  204.         };
  205.     };
  206.     printf(" (%d).\n", deviceType);
  207.     
  208.     printf("Slot number is %d, other is %d.\n", slotNumber, slotOtherInfo);
  209.  
  210.     OTGetUserPortNameFromPortRef(portInfoRecord.fRef, userFriendlyName);
  211.     printf("OTGetUserPortNameFromPortRef = %#s\n", userFriendlyName);
  212.  
  213. //    Returns the location for the icon familly representing the port.
  214. //    Returns false if the port has no icon
  215. //
  216. // Boolean OTGetPortIconFromPortRef(OTPortRef ref, OTResourceLocator* iconLocation);
  217.  
  218.     printf("\n\n");
  219. }
  220.  
  221.  
  222. static void DumpPortRecords(void)
  223. {
  224.   OTPortRecord  portInfoRecord;
  225.   size_t          portIndex;
  226.     
  227.   portIndex = 0;
  228.   while (OTGetIndexedPort(&portInfoRecord, portIndex)) {
  229.       DumpPortRecord(portInfoRecord);
  230.     portIndex += 1;
  231.   } 
  232. }
  233.  
  234.  
  235. void main(void)
  236. {
  237.     OSStatus err;
  238.     long gestaltResponse;
  239.     
  240.     printf("OTDumpPortRegistry -- Dumps the Open Transport port registry to stdout\n\n");
  241.  
  242.     if (Gestalt(gestaltOpenTptVersions, &gestaltResponse) == noErr) {
  243.         printf("gestaltOpenTptVersions = %08x\n", gestaltResponse);
  244.     } else {
  245.         printf("gestaltOpenTptVersions = <undefined> (version prior to OT 1.1)\n");
  246.     }
  247.     (void) Gestalt(gestaltMachineType, &gestaltResponse);
  248.     printf("gestaltMachineType = %08x\n", gestaltResponse);
  249.     (void) Gestalt(gestaltSystemVersion, &gestaltResponse);
  250.     printf("gestaltSystemVersion = %08x\n", gestaltResponse);
  251.  
  252.     printf("\nHello Cruel World!\n\n\n");
  253.     
  254.     err = InitOpenTransport();
  255.     
  256.     if (err == noErr) {
  257.     
  258.         DumpPortRecords();
  259.         
  260.         CloseOpenTransport();
  261.     }
  262.     
  263.     if (err == noErr) {
  264.         printf("Success.\n");
  265.     } else {
  266.         printf("Failed with error %d.\n", err);
  267.     }
  268.     printf("Done.  Press command-Q to Quit.\n");
  269. }
  270.